package com.example.qjia.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.qjia.R;
import com.example.qjia.data.HolidayMessage;
import com.example.qjia.data.romotedata.AbsenceSlip;
import com.example.qjia.ui.activity.HolidayDetailActivity;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.zip.Inflater;

/**
 * Created by ying_xiang on 2016/8/12.
 */
public class HolidayAdapter extends RecyclerView.Adapter<ViewHolder> implements View.OnClickListener {

    private static final int TYPE_ITEM=1;
    private static final int TYPE_FOOTER=0;
    private List<AbsenceSlip> list;
    private Context mcontext;
    private String nothing=" ";
    private int[] Imgs;

    public HolidayAdapter(List<AbsenceSlip> list, Context mcontext) {
        this.list = list;
        this.mcontext = mcontext;
        Imgs=new int[]{R.mipmap.agree,R.mipmap.agree,R.mipmap.agree};


    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if(viewType==TYPE_ITEM) {
            View view = LayoutInflater.from(mcontext).inflate(R.layout.item_holiday, parent, false);
            return new ItemHolder(view);
        }else if(viewType==TYPE_FOOTER){
            View view = LayoutInflater.from(mcontext).inflate(R.layout.item_foot, parent, false);
            return new FooterHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (holder instanceof ItemHolder){
            ItemHolder itemHolder=(ItemHolder)holder;
            itemHolder.reasonText.setText(list.get(position).getReason());
            itemHolder.checkImg.setImageResource(Imgs[list.get(position).getStatus()]);//设置审核图
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm");
            String time=format.format(list.get(position).getTimeApply());
            itemHolder.requestTime.setText(time);
            itemHolder.holiday_item.setTag(list.get(position).getAid());//假条id
            itemHolder.holiday_item.setOnClickListener(this);


        }
        if(holder instanceof  FooterHolder){
            FooterHolder footerHolder=(FooterHolder)holder;

            if(list.size()<11)footerHolder.footly.setVisibility(View.INVISIBLE);
            else footerHolder.footly.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public int getItemCount() {
        return list.size()+1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public void onClick(View view) {
        Bundle bundle=new Bundle();
        bundle.putInt("holidayId",(Integer)view.getTag()); //传递假条id
        mcontext.startActivity(new Intent(mcontext, HolidayDetailActivity.class).putExtras(bundle));

    }

    static class ItemHolder extends RecyclerView.ViewHolder{
        CardView holiday_item;
        TextView reasonText;
        TextView requestTime;
        ImageView checkImg;
        public ItemHolder(View itemView) {
            super(itemView);
            holiday_item=(CardView)itemView.findViewById(R.id.holiday_card_view);
            reasonText=(TextView)itemView.findViewById(R.id.reason);
            requestTime=(TextView)itemView.findViewById(R.id.requestTime);
            checkImg=(ImageView)itemView.findViewById(R.id.checkImg);


        }
    }
    static class FooterHolder extends RecyclerView.ViewHolder{
        LinearLayout footly;

        TextView nothing;
        public FooterHolder(View itemView) {
            super(itemView);
            footly=(LinearLayout)itemView.findViewById(R.id.foot_ly);


        }
    }
}
